package com.miyake.dsp;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.scilab.modules.javasci.JavasciException;

import com.miyake.rest.DataColumn;
import com.miyake.rest.Value;
import com.miyake.rest.WebServiceProperty;
import com.miyake.rest.Value.SubUnit;

/**
 * Servlet implementation class ScilabServlet
 */
public class ScilabServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ScilabServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		WebServiceProperty prop = WebServicePropertyBuilder.getInstance().get();
		
		prop.getValues().add(new Value("Command", "Command", "ret=convol(data1,data2);", 
				SubUnit.Nano, "None"));
		for (Integer i = 1; i <= 2; i++) {
			prop.getData().add(new DataColumn("data" + i.toString()));
		}
		
		response.getWriter().print(prop.toXML());
		response.getWriter().flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		WebServiceProperty prop = WebServiceProperty.fromXml(request.getInputStream());
		
		try {
			WebServiceProperty ret = ScilabEngine.getInstance().process(prop);
			response.getWriter().print(ret.toXML());
		} catch (JavasciException e) {
			response.getWriter().print(e.getMessage());
		}
		
		response.getWriter().flush();
	}

}
